package com.longxi.tianjintianbao.ui.presenter.visitor;

import android.support.v7.widget.PopupMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.ui.adapter.VisitorListPagerAdapter;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.fragment.visitor.VisitorListFragment;
import com.longxi.tianjintianbao.ui.view.visitor.IVisitorListAtView;
import com.longxi.tianjintianbao.utils.MyUtils;

import java.util.ArrayList;
import java.util.List;

public class VisitorListAtPresenter extends BasePresenter<IVisitorListAtView> {

    private VisitorListPagerAdapter visitorListPagerAdapter;
    private List<TextView> textViews = new ArrayList<>();
    PopupMenu popup;

    private void initVariable() {
        mCategoryList = new ArrayList<>();
        mCategoryList.add("未到访");
        mCategoryList.add("到访中");
        mCategoryList.add("已离开");
    }

    //tab 标签页
    private List<String> mCategoryList;

    public List<String> getCategoryList() {
        if (mCategoryList == null) {
            mCategoryList = new ArrayList<>();
        }
        return mCategoryList;
    }

    public VisitorListAtPresenter(BaseActivity context) {
        super(context);
    }

    public void setBar() {
        mContext.setTitle("访客登记");
        mContext.setTvLeft(MyUtils.getString(R.string.home));
        mContext.setTvRight("在线登记");
        mContext.mTvRight.setVisibility(View.VISIBLE);
        initVariable();

        textViews.add(getView().getTvNotVisiting());
        textViews.add(getView().getTvVisit());
        textViews.add(getView().getTvGoneAway());
    }

    public void initViewPager(){
        visitorListPagerAdapter = new VisitorListPagerAdapter(mContext.getSupportFragmentManager(), getCategoryList());
        getView().getViewPager().setAdapter(visitorListPagerAdapter);
        getView().getViewPager().setOffscreenPageLimit(getCategoryList().size() - 1);
    }

    public void refreshFragmentUi() {
        VisitorListFragment visitorListFragment = (VisitorListFragment) visitorListPagerAdapter.getCurrentFragment();
        visitorListFragment.refreshReceiptList();
    }

    public void onBarSwitch(int position) {
        getView().getViewPager().setCurrentItem(position);
        for (int i = 0; i < textViews.size(); i++) {
            if(i == position){
                textViews.get(i).setTextColor(MyUtils.getResource().getColor(R.color.color_onpress_red));
            }else{
                textViews.get(i).setTextColor(MyUtils.getResource().getColor(R.color.color_text3));
            }
        }
    }

    /**
     * 弹出选择
     */
    public void popFilter() {
        if(popup == null){
            popup = new PopupMenu(mContext, getView().getIvFilter());
            popup.getMenuInflater().inflate(R.menu.popup_filter_week, popup.getMenu());
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.a_week:
//                            listener.onClick(getAdapterPosition());
                            popup.dismiss();
                            break;
                        case R.id.all:
                            popup.dismiss();
                            break;
                    }
                    return true;
                }
            });
        }else{
            popup.show();
        }

    }

    @Override
    public void detachView() {
        super.detachView();
        this.visitorListPagerAdapter = null;
        this.popup = null;
        this.textViews = null;
    }
}
